local skels = {}

local skel = fk.CreateSkill {
  name = "rmt__juxie",
}

skel:addEffect(fk.CardUseFinished, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and data.card.type == Card.TypeBasic
    and table.every(player.room.alive_players, function (p) return not p.dying end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local _, dat = room:askToUseActiveSkill(player, {
      skill_name = "rmt__juxie_active", prompt = "#rmt__juxie-ask"
    })
    if dat then
      event:setCostData(self, {choice = dat.interaction, tos = dat.targets, cards = dat.cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event:getCostData(self).choice == "rmt__juxie_draw" then
      room:handleAddLoseSkills(player, "rmt__chanyuan")
      player:drawCards(1, self.name)
    else
      local to = event:getCostData(self).tos[1]
      room:throwCard(event:getCostData(self).cards, skel.name, player, player)
      if not to.dead then
        local num = 3 - to:getHandcardNum()
        if num > 0 then
          to:drawCards(num, skel.name)
        end
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__juxie"] = "拘邪",
  [":rmt__juxie"] = "你使用基本牌结算后，若此时没有角色处于濒死状态，你可以选择一项：1.获得〖缠怨〗，然后摸一张牌；2.弃置一张♠牌并令一名角色将手牌摸至三张。",
  ["rmt__juxie_draw"] = "获得“缠怨”并摸1张牌",
  ["rmt__juxie_throw"] = "弃置♠牌令一名角色手牌摸至3张",
  ["#rmt__juxie-ask"] = "拘邪：你可选一项",
  ["rmt__juxie_active"] = "拘邪",

  ["$rmt__juxie1"] = "授汝上清灵章，丹宝飞步，卫道除魔。",
  ["$rmt__juxie2"] = "开明灵童，总御火兵，斩邪束妖。",
}

local skel2 = fk.CreateSkill {
  name = "rmt__juxie_active",
}

skel2:addEffect("active", {
  interaction = function (self, player)
    local all_choices = {"rmt__juxie_draw", "rmt__juxie_throw"}
    local choices = table.simpleClone(all_choices)
    if player:hasSkill("rmt__chanyuan", true) then
      table.remove(choices, 1)
    end
    return UI.ComboBox { choices = choices, all_choices = all_choices }
  end,
  card_filter = function (self, player, to_select, selected)
    return self.interaction.data == "rmt__juxie_throw"
    and #selected == 0 and Fk:getCardById(to_select).suit == Card.Spade
    and not player:prohibitDiscard(Fk:getCardById(to_select))
  end,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return self.interaction.data == "rmt__juxie_throw" and #selected == 0 and #selected_cards == 1
  end,
  feasible = function (self, player, selected, selected_cards, card)
    return self.interaction.data ~= "rmt__juxie_throw" or (#selected == 1 and #selected_cards == 1)
  end,
})

table.insert(skels, skel)
table.insert(skels, skel2)

return skels
